package com.example.foodwebbackend.controller;

import com.example.foodwebbackend.dto.ApiResponse;
import com.example.foodwebbackend.dto.PageRequest;
import com.example.foodwebbackend.dto.PageResponse;
import com.example.foodwebbackend.entity.FoodCategory;
import com.example.foodwebbackend.service.FoodCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/categories")
@CrossOrigin(origins = "*")
public class AdminCategoryController {

    @Autowired
    private FoodCategoryService foodCategoryService;

    /**
     * 分页获取所有分类（管理员）
     */
    @PostMapping("/page")
    public ApiResponse<PageResponse<FoodCategory>> getCategoriesWithPagination(@RequestBody PageRequest request) {
        try {
            PageResponse<FoodCategory> response = foodCategoryService.getCategoriesWithPagination(request);
            return ApiResponse.success("获取分类列表成功", response);
        } catch (Exception e) {
            return ApiResponse.error("获取分类列表失败：" + e.getMessage());
        }
    }

    /**
     * 搜索分类
     */
    @GetMapping("/search")
    public ApiResponse<List<FoodCategory>> searchCategories(@RequestParam String keyword) {
        try {
            List<FoodCategory> categories = foodCategoryService.searchCategories(keyword);
            return ApiResponse.success("搜索分类成功", categories);
        } catch (Exception e) {
            return ApiResponse.error("搜索分类失败：" + e.getMessage());
        }
    }

    /**
     * 添加分类
     */
    @PostMapping
    public ApiResponse<FoodCategory> addCategory(@RequestBody FoodCategory category) {
        try {
            FoodCategory newCategory = foodCategoryService.addCategory(category);
            return ApiResponse.success("添加分类成功", newCategory);
        } catch (Exception e) {
            return ApiResponse.error("添加分类失败：" + e.getMessage());
        }
    }

    /**
     * 更新分类
     */
    @PutMapping("/{id}")
    public ApiResponse<FoodCategory> updateCategory(@PathVariable Integer id, @RequestBody FoodCategory category) {
        try {
            category.setCategoryId(id);
            FoodCategory updatedCategory = foodCategoryService.updateCategory(category);
            return ApiResponse.success("更新分类成功", updatedCategory);
        } catch (Exception e) {
            return ApiResponse.error("更新分类失败：" + e.getMessage());
        }
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteCategory(@PathVariable Integer id) {
        try {
            foodCategoryService.deleteCategory(id);
            return ApiResponse.success("删除分类成功", null);
        } catch (Exception e) {
            return ApiResponse.error("删除分类失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除分类
     */
    @DeleteMapping("/batch")
    public ApiResponse<String> batchDeleteCategories(@RequestBody List<Integer> categoryIds) {
        try {
            foodCategoryService.batchDeleteCategories(categoryIds);
            return ApiResponse.success("批量删除分类成功", null);
        } catch (Exception e) {
            return ApiResponse.error("批量删除分类失败：" + e.getMessage());
        }
    }

    /**
     * 获取分类统计
     */
    @GetMapping("/stats")
    public ApiResponse<Integer> getCategoryStats() {
        try {
            int count = foodCategoryService.getCategoryCount();
            return ApiResponse.success("获取分类统计成功", count);
        } catch (Exception e) {
            return ApiResponse.error("获取分类统计失败：" + e.getMessage());
        }
    }
} 